package com.mijie.homi.admin.web.mood;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mijie.core.ApplicationProperties;
import com.mijie.homi.admin.service.mood.MoodInfoService;
import com.mijie.homi.common.constant.HomiConstants;
import com.mijie.homi.common.domain.mood.MoodInfo;
import com.mijie.homi.common.util.HomiUtil;
import com.mijie.util.FileUploadUtil;
import com.mijie.util.ImageUtil;

@Controller
@RequestMapping("/moodinfo")
public class MoodInfoController {

	@Autowired
	private MoodInfoService service;
	private Logger logger = Logger.getLogger(MoodInfoController.class);
	
	@RequestMapping("/list")
	public String list(Model model){
		List<MoodInfo> list = service.listMoodInfo();
		for(MoodInfo info : list){
			info.setSmallIcon(HomiUtil.getRealUrl(info.getSmallIcon()));
			info.setLargeIcon(HomiUtil.getRealUrl(info.getLargeIcon()));
		}
		model.addAttribute("list",list);
		return "moodinfo/list";
	}
	
	@RequestMapping("/create")
	public String create(Model model){
		return "moodinfo/create";
	}
	
	@RequestMapping("/save")
	public String save(HttpServletRequest request,MoodInfo info) throws Exception{
		info.setLargeIcon("");
		info.setSmallIcon("");
		settingIconIfUpload(request,info);
		service.addMoodInfo(info);
		return "redirect:/moodinfo/list.shtml";
	}
	
	@RequestMapping("/edit")
	public String edit(Model model,int id){
		MoodInfo info = service.getMoodInfoById(id);
		info.setSmallIcon(HomiUtil.getRealUrl(info.getSmallIcon()));
		info.setLargeIcon(HomiUtil.getRealUrl(info.getLargeIcon()));
		model.addAttribute("info",info);
		return "moodinfo/edit";
	}
	
	
	@RequestMapping("/update")
	public String update(HttpServletRequest request,MoodInfo info) throws Exception{
		settingIconIfUpload(request,info);
		service.updateMoodInfo(info);
		return "redirect:/moodinfo/list.shtml";
	}
	
	private void settingIconIfUpload(HttpServletRequest request,MoodInfo info) throws Exception{
		if(request instanceof MultipartHttpServletRequest){
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile largeFile = multipartRequest.getFile("largeIconFile");
			if(largeFile==null || largeFile.isEmpty()) return;
			MultipartFile smallFile = multipartRequest.getFile("smallIconFile");
			if(smallFile==null || smallFile.isEmpty()) return;
			if(!FileUploadUtil.validate(largeFile, HomiConstants.IMAGE_SUFFIX) ||
			   !FileUploadUtil.validate(smallFile, HomiConstants.IMAGE_SUFFIX) ){
				throw new  Exception("mood icon upload fail , because image suffix illegal");
			}
			
			String currentTime = String.valueOf(System.currentTimeMillis());
			String resPath = ApplicationProperties.get("resource.path");
			String largeIconPath = ApplicationProperties.get("moodicon.large.home");
			String smallIconPath = ApplicationProperties.get("moodicon.small.home");
			
			String saveName = FileUploadUtil.upload(largeFile, resPath+largeIconPath,currentTime);
			if(saveName==null || saveName.trim().isEmpty()) throw new Exception("mood icon upload fail.");
			info.setLargeIcon(largeIconPath+saveName);
			
			saveName = FileUploadUtil.upload(smallFile, resPath+smallIconPath,currentTime);
			if(saveName==null || saveName.trim().isEmpty()) throw new Exception("mood icon upload fail.");
			info.setSmallIcon(smallIconPath+saveName);
		}
	}
}
